<!DOCTYPE html>
<html  lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用layui</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>


<body>


<div class="layui-container">
    <form class="layui-form layui-form-pane" action="">
          <div class="layui-row">
              <div class="layui-col-md3">
                  起始日期
                  <div class="layui-input-inline">
                      <input type="text" class="layui-input" id="test6" name="Begintime" >
                  </div>
              </div>
              <div class="layui-col-md3">
                  终止日期
                  <div class="layui-input-inline">
                      <input type="text" class="layui-input" id="test7" name="Updatetime" >
                  </div>
              </div>
              <div class="layui-col-md3">

                 标题
                  <div class="layui-input-inline">
                      <input type="text" class="layui-input" id="title" name="title">
                  </div>
              </div>
              <div class="layui-col-md3">
                  <button type="submit" class="layui-btn" lay-submit  lay-filter="data-search-btn">
                      <i class="layui-icon">&#xe642;</i> 搜索
                  </button>
              </div>
          </div>
    </form>
</div>


<table id="demo"  lay-filter="test"></table>

<div class="layui-btn-container" id="toolbarDemo" style="display: none">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
</div>

<div id="barDemo" style="display: none">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</div>

</body>
</html>
<script type="text/javascript">
    layui.use(['table','laydate','form'], function(){
        var table = layui.table;
        var laydate = layui.laydate;
        var form =layui.form

        laydate.render({
            elem: '#test6'
        })
        laydate.render({
            elem: '#test7'
        })

        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 400
            ,url: 'findAll' //数据接口
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,page:true
            ,cols: [ [//注意这里【【之前要有一个空格
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'bid', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{field: 'title', title: '标题', width:80}
                ,{field: 'avatar', title: '头像', width:80, sort: true}
                ,{field: 'content', title: '内容', width:80}
                ,{field: 'begintime', title: '开始时间', width: 177}
                ,{field: 'updatetime', title: '截至时间', width: 177}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ] ]

        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        //这里做条件查询
        form.on('submit(data-search-btn)',function (data) {
            var result = JSON.stringify(data.field);
            table.reload('demo', {
                page: {
                    curr: 1
                }
                , where: {
                    QueryBlog: result
                }
            }, 'data');
            return false
        })
    });
</script>
